# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import os, stat
from tools.misc import debug
from reportlab import rl_config
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase import ttfonts
from reportlab.lib.fonts import addMapping

def rl_isreg(filename, dirname):
    try:
        st = os.stat(os.path.join(dirname, filename))
    except OSError, reason:
        if reason.errno == 2:
            return False
        raise
    return stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)

for dirname in rl_config.TTFSearchPath:
    for root, dirs, files in os.walk(dirname): #@UnusedVariable
        for file in [x for x in files
                     if x.lower().endswith('.ttf') and rl_isreg(x, root)
                     ]:
            filename = os.path.join(root, file)
            try:
                face = ttfonts.TTFontFace(filename)
                face.extractInfo(1)
                pdfmetrics.registerFont(ttfonts.TTFont(face.name, filename, asciiReadable=0))
                addMapping(face.familyName, face.bold, face.italic, face.name)
            except:
                pass

from trml2pdf import parseString, parseNode
